/**
 * 
 */
package edu.pku.sei.modeler.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;

import edu.pku.notation.runtime.draw2d.RoundedRectangleFigure;
import edu.pku.notation.runtime.draw2d.TextFigure;
import edu.pku.sei.modeler.uml.ResourceFactory;

/**
 * @author YanYan
 * 
 */
public class ComplexStateFigure extends CommonFigure {
	public IFigure root;

	public IFigure body;

	private Color lineColor = ColorConstants.blue;
	
	public IFigure getBody() {
		return body;
	}

	private TextFigure nameLabel = null;

	public ComplexStateFigure() {

		super();

		XYLayout layout = new XYLayout();
		setLayoutManager(layout);

		root = new RoundedRectangleFigure();
		((RoundedRectangleFigure) root).setCornerHeight(40);
		((RoundedRectangleFigure) root).setCornerWidth(40);
		ToolbarLayout rootLayout = new ToolbarLayout();
		rootLayout.setVertical(true);
		root.setLayoutManager(rootLayout);
		root.setForegroundColor(ColorConstants.blue);

		nameLabel = new TextFigure();
		nameLabel.setOpaque(false);
		nameLabel.setFont(ResourceFactory.getHeadFont());
		root.add(nameLabel);

		body = new RoundedRectangleFigure();
		((RoundedRectangleFigure) body).setCornerHeight(20);
		((RoundedRectangleFigure) body).setCornerWidth(20);
		XYLayout xyL = new XYLayout();
		body.setLayoutManager(xyL);
		body.setOpaque(true);
		root.add(body);

		add(root);
	}

	public void changeLineColor(Color color) {
		root.setForegroundColor(color);
		lineColor = color;
	}
	
	public void paintFigure(Graphics graphics) {
		graphics.pushState();
		graphics.setForegroundColor(ColorConstants.white);
		// graphics.fillGradient(getBounds(), true);
		graphics.fillOval(getBounds());
		graphics.popState();
	}

	public void paint(Graphics graphics) {
		super.paint(graphics);

		graphics.pushState();
		graphics.setAntialias(SWT.ON);
		Rectangle figureArea = getBounds();
		int width = figureArea.width;
		graphics.setForegroundColor(lineColor);

		Point left = new Point(figureArea.x, figureArea.y + 22);
		Point right = new Point(figureArea.x + width, figureArea.y + 22);

		graphics.drawLine(left, right);

		graphics.popState();
	}

	public TextFigure getNameLable() {
		return nameLabel;
	}

	public void setBounds(Rectangle rect) {
		super.setBounds(rect);
		if (root != null)
			root.setSize(rect.width, rect.height);
		if (body != null) {
			body.setSize(rect.width, rect.height);
		}
	}

	@Override
	public void changeColor(Color color) {
		root.setBackgroundColor(color);
	}
}
